package com.shinbada.modules.competitor.web;

import com.shinbada.excel.modle.ImportExcelResult;
import com.shinbada.excel.modle.UploadModle;
import com.shinbada.modules.competitor.entity.DTO.CompetitorProductDTO;
import com.shinbada.modules.competitor.entity.DTO.ParamModelSelectionDTO;
import com.shinbada.modules.competitor.entity.VO.CompetitorDataItemVO;
import com.shinbada.modules.competitor.entity.VO.CompetitorNoumenonResultVO;
import com.shinbada.modules.competitor.entity.VO.CompetitorResultVO;
import com.shinbada.modules.competitor.utils.TreeUtilCommon;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.shinbada.common.utils.DateUtils;
import com.shinbada.common.json.AjaxJson;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.modules.competitor.entity.CompetitorProduct;
import com.shinbada.modules.competitor.service.CompetitorProductService;

import com.shinbada.excel.ExcelKit;

/**
 * 产品基础表Controller
 * @author initcap
 * @version 2021-08-04
 */
@RestController
@RequestMapping(value = "/competitor/competitorProduct")
@Api(tags = "产品基础表")
public class CompetitorProductController extends BaseController {

	@Autowired
	private CompetitorProductService competitorProductService;

	@ModelAttribute
	public CompetitorProduct get(@RequestParam(required=false) String id) {
		CompetitorProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = competitorProductService.get(id);
		}
		if (entity == null){
			entity = new CompetitorProduct();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@GetMapping("list")
	@ApiOperation("产品基础表列表数据")
	public AjaxJson list(CompetitorProduct competitorProduct, HttpServletRequest request, HttpServletResponse response) {
		Page<CompetitorProduct> page = competitorProductService.findPage(new Page<>(request, response), competitorProduct);
		return AjaxJson.success().put("page",page);
	}

	@RequiresPermissions("user")
	@GetMapping("queryById")
	@ApiOperation("根据Id获取产品基础表数据")
	public AjaxJson queryById(CompetitorProduct competitorProduct) {
		return AjaxJson.success().put("competitorProduct", competitorProduct);
	}

	@RequiresPermissions(value={"competitor:competitorProduct:add","competitor:competitorProduct:edit"},logical=Logical.OR)
	@PostMapping("save")
	@ApiOperation("保存产品基础表")
	public AjaxJson save(CompetitorProduct competitorProduct) {
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(competitorProduct);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		competitorProductService.save(competitorProduct);
		return AjaxJson.success("保存产品基础表成功");
	}

	@RequiresPermissions("competitor:competitorProduct:del")
	@DeleteMapping("delete")
	@ApiOperation("批量删除产品基础表")
	public AjaxJson delete(String ids) {
		String[] idArray =ids.split(",");
		for(String id : idArray){
			competitorProductService.delete(competitorProductService.get(id));
		}
		return AjaxJson.success("删除产品基础表成功");
	}

	@RequiresPermissions("competitor:competitorProduct:export")
	@GetMapping("export")
	@ApiOperation("导出产品基础表")
	public void export(CompetitorProduct entity, HttpServletResponse response) {
		ExcelKit.$Export(CompetitorProduct.class, response).downXlsx(competitorProductService.findList(entity), false);
	}

	@RequiresPermissions("competitor:competitorProduct:import")
	@GetMapping("template")
	@ApiOperation("导出产品基础表模版")
	public void export(HttpServletResponse response) {
		ExcelKit.$Export(CompetitorProduct.class, response).downXlsx(new ArrayList<>(), true);
	}

	@RequiresPermissions("competitor:competitorProduct:import")
	@PostMapping(value = "import")
	@ApiImplicitParam(name = "modle", paramType = "query", allowableValues = "skip,exit,cover")
	@ApiOperation("导入文件,下载模版为/export")
	public AjaxJson uploadExcel(MultipartFile file, UploadModle modle) {
		if (shinProperites.isDemoMode()) {
			return AjaxJson.error("演示模式，不允许操作！");
		}
		ImportExcelResult<CompetitorProduct> result = competitorProductService.uploadExcel(file, modle);
		return AjaxJson.success(result);
	}
	@RequiresPermissions("user")
	@GetMapping("productList")
	@ApiOperation("竞品对比数据获取")
	public AjaxJson productList(CompetitorProductDTO competitorProductDTO) throws Exception {
		List<CompetitorResultVO> list = competitorProductService.productList(competitorProductDTO);
		for (CompetitorResultVO competitorResultVO : list) {
			Map<String, CompetitorDataItemVO> map = competitorResultVO.getCompetitorDataItemVOS().stream()
					.collect(Collectors.toMap(CompetitorDataItemVO::getDataItemId, o -> o, (o, o1) -> o));
			competitorResultVO.setCompetitorDataItemVOMap(map);
			competitorResultVO.setCompetitorDataItemVOS(null);
		}
		return AjaxJson.success().put("data",list);
	}
	@RequiresPermissions("user")
	@PostMapping("modelSelection/{type}")
	@ApiOperation("竞品参数选型")
	public AjaxJson modelSelection(@PathVariable String type,@RequestBody List<ParamModelSelectionDTO> paramModelSelectionDTOList) {
		List<CompetitorResultVO> list = competitorProductService.modelSelection(type,paramModelSelectionDTOList);
		return AjaxJson.success().put("data",list);
	}
	@RequiresPermissions("user")
	@GetMapping("competitorRecommend")
	@ApiOperation("竞品推荐")
	public AjaxJson competitorRecommend(CompetitorProductDTO competitorProductDTO) {
		List<CompetitorResultVO> list = competitorProductService.competitorRecommend(competitorProductDTO);
		return AjaxJson.success().put("data",list);
	}
}
